<template>
    <view class="cu-modal" :class="showDialog ? 'show': '' ">
        <view class="cu-dialog text-grey" style="width:85%;">
            <view class="cu-bar bg-white justify-end">
                <view class="content">请求授权当前位置</view>
                <view class="action" @tap="hide">
                    <text class="cuIcon-close"></text>
                </view>
            </view>
            <view class="padding">
                <view class="iconfont cuIcon- icon-xiaochengxu text-grey round" style="font-size:80rpx;"></view>
                <view class="margin-top">
                    <view class="text-grey">我们需要您的地址信息来设置您的地址信息</view>
                </view>
            </view>
            <view class="cu-bar padding">
                <view class="action margin-0 flex-sub  solid-left" @tap="">
                    <button class="cu-btn lg bg-green round response"  @opensetting="opensetting" open-type="openSetting">授权</button>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
    export default {
        props:{
        },
        data(){
           return  {
               showDialog:false
            }
        },
        methods:{
            show(){
                this.showDialog = true
            },
            hide(){
                this.showDialog = false
            },
            init(){
                if(!this.$tools.user()){
                    console.log('无登录不弹')
                    return
                }
                if(this.$cache.get('location')){
                    console.log("有了不弹")
                    return
                }
                if(this.$cache.get('locationTwiceRefuse')){
                    console.log("二次拒绝不要弹了")
                    return
                }
                let scope = 'userLocation'

                this.$thirdApi.getSetting(scope).then(res=>{
                    if(res){
                        console.log('位置授权了已经')
                        this.doGetLocationRes()
                    }else{
                        console.log('位置没授权')
                        //拉起授权
                        this.$thirdApi.authorize(scope).then(res=>{
                            if(res){
                                console.log('当用户地址授权了的话')
                                this.doGetLocationRes(res)

                            }else{
                                //当用户地址授权拒绝的话
                                console.log('当用户地址授权拒绝的话 继续弹出1次')
                                this.showDialog = true;

                            }
                        })

                    }
                })

            },
            //打开权限设置
            opensetting(e){
                console.log('bindopensetting')

                if (!e.detail.authSetting['scope.userLocation']){
                    console.log('用户继续拒绝了')
                    this.$cache.put('locationTwiceRefuse',1,7*24*3600) //保存7天

                }else {
                    console.log('用户第二次同意了')
                    this.doGetLocationRes()
                }
                this.showDialog = false
                this.$parent.fetchData()

            },
            doGetLocationRes(){
                    this.$thirdApi.getLocation().then(res=>{
                        if(res){
                            let {latitude,longitude} = res
                            this.$api.location({location:{latitude,longitude},showLoading:0}).then(res=>{
                                if(res){
                                   // this.$tools.msg('成功')
                                }
                            })
                            this.$cache.put('location',res,7*24*3600);
                        }
                    })
            }
        },
        created() {
            this.init()
        }
    }
</script>

<style scoped>

</style>
